package com.sharer.last.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sharer.last.base.enums.BusinessExceptionCode;
import com.sharer.last.base.enums.ExceptionPrefixParamEnum;
import com.sharer.last.base.exception.BusinessException;
import com.sharer.last.base.model.PageVO;
import com.sharer.last.base.support.QueryCondition;
import com.sharer.last.base.utils.ConvertUtil;
import com.sharer.last.mapper.ExceptionLogMapper;
import com.sharer.last.model.dto.log.ExceptionLogChangeDTO;
import com.sharer.last.model.dto.log.ExceptionLogQueryDTO;
import com.sharer.last.model.dto.UserAgentDTO;
import com.sharer.last.model.po.ExceptionLogPO;
import com.sharer.last.model.vo.log.ExceptionLogVO;
import com.sharer.last.service.ExceptionLogService;
import com.sharer.last.utils.BeanUtil;
import com.sharer.last.utils.IpAddressUtils;
import com.sharer.last.utils.UserAgentUtils;
import com.sharer.last.wrapper.ExceptionLogWrapper;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;

/**
 * 文件注释地方
 *
 * @ClassName 异常日志业务层实现
 * @Author wangjin
 * @Date 2023/5/5 20:46
 * @Description
 * @Version 1.0
 */
@Service
public class ExceptionLogServiceImpl extends ServiceImpl<ExceptionLogMapper, ExceptionLogPO> implements ExceptionLogService {
    @Resource
    ExceptionLogMapper exceptionLogMapper;

    @Resource
    ExceptionLogWrapper exceptionLogWrapper;

    @Resource
    UserAgentUtils userAgentUtils;

    /**
     * 分页查询
     *
     * @param queryDTO
     * @return
     */
    @Override
    public PageVO<ExceptionLogVO> selectListByPage(ExceptionLogQueryDTO queryDTO) {
        LambdaQueryWrapper<ExceptionLogPO> qw   = this.buildQueryWrapper(queryDTO);
        IPage<ExceptionLogPO>              page = this.page(QueryCondition.getPage(queryDTO.getPage()), qw);
        return exceptionLogWrapper.toPageVO(page);
    }

    private LambdaQueryWrapper<ExceptionLogPO> buildQueryWrapper(ExceptionLogQueryDTO queryDTO) {
        LambdaQueryWrapper<ExceptionLogPO> qw        = new LambdaQueryWrapper<>();
        String                             startTime = queryDTO.getStartTime();
        if (startTime != null) {
            qw.gt(ExceptionLogPO::getCreateTime, startTime);
        }
        String endTime = queryDTO.getEndTime();
        if (endTime != null) {
            qw.lt(ExceptionLogPO::getCreateTime, endTime);
        }
        return qw;
    }

    /**
     * 创建异常日志
     *
     * @param dto
     */
    @Override
    public void createExceptionLog(ExceptionLogChangeDTO dto) {
        dto.setId(null);
        ExceptionLogPO po           = BeanUtil.copy(dto, ExceptionLogPO.class);
        String         ipSource     = IpAddressUtils.getCityInfo(dto.getIp());
        UserAgentDTO   userAgentDTO = userAgentUtils.parseOsAndBrowser(dto.getUserAgent());
        po.setIpSource(ipSource);
        po.setOs(userAgentDTO.getOs());
        po.setBrowser(userAgentDTO.getBrowser());
        if (!this.save(po)) {
            throw new BusinessException(BusinessExceptionCode.DATA_REMOVE_FAIL, ExceptionPrefixParamEnum.EXCEPTION_LOG);
        }
    }

    /**
     * 删除异常日志
     *
     * @param id
     */
    @Override
    public void removeExceptionLogById(Long id) {
        if (ConvertUtil.toInt(id) < 1) {
            throw new BusinessException(BusinessExceptionCode.ID_IS_NULL, ExceptionPrefixParamEnum.EXCEPTION_LOG);
        }
        // 查询数据是否存在
        ExceptionLogPO exceptionLogPO = this.getById(id);
        if (exceptionLogPO == null) {
            throw new BusinessException(BusinessExceptionCode.DATA_IS_EMPTY, ExceptionPrefixParamEnum.EXCEPTION_LOG);
        }
        if (!this.removeById(id)) {
            throw new BusinessException(BusinessExceptionCode.DATA_REMOVE_FAIL, ExceptionPrefixParamEnum.EXCEPTION_LOG);
        }
    }
}
